# This file is part of OrangeSpider. OrangeSpider is a meta model based
# development tool.
# 
# OrangeSpider is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# OrangeSpider is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with OrangeSpider.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2009 Stefan Nikolaus <OrangeSpiderMaster@googlemail.com>


class Types:
  def __init__(self):
     pass


# Displays a type as string representation
def UiTypes_str(x, typename):
  return str(x)
def UiTypes_text(x, typename):
  return str(x)[:20]
def UiTypes_typename(x, typename):
  if x == "":
    return "(empty)"
  else:
    return "(" + typename + ")"
def UiTypes_datetime(x, typename):
  if isinstance(x, tuple):
    return "%2d-%02d-%02d %02d:%02d:%02d" % x
  else:
    return x
  
class UiTypes:

  convertMap = {
        "String": UiTypes_str,
        "Integer": UiTypes_str,
        "Boolean": UiTypes_str,
        "Float": UiTypes_str,
        "Picture": UiTypes_typename,
        "Text": UiTypes_text,
        "DateTime": UiTypes_datetime,
        "TimeStamp": UiTypes_datetime,
        "Color": UiTypes_str,
        "Font": UiTypes_str,
        "Blob": UiTypes_typename,
        "FileString": UiTypes_str,
        "Directory": UiTypes_str,
        "Url": UiTypes_str,
        "Size": UiTypes_str,
        "Point": UiTypes_str,
        "Rect": UiTypes_str,
        "Flags": UiTypes_str
      }
  def __init__(self):
      pass

  @staticmethod
  def asTextValue(object, property):
     if object.isExtensionEntity():
         metaType = object.intention.metaProperties[property].metaType
         if metaType.isMetaEnum():
             return object[property]
         else:
             metaTypeName = metaType["Name"]
             return UiTypes.convertMap[metaTypeName](object[property],
                                                     metaTypeName) 
     else:
         return object[property]

  @staticmethod
  def setValue(object, property, value):
     pass


def getTypedValue(mt, value):
    assert(mt.isMetaType())
    name = mt["Name"]

    #print name, value, type(value)
    def isString(value):
        return isinstance(value, str) or isinstance(value, unicode)
  
    def getTuple(s, typ, length, default = ""):
        if isString(s):
            if s == "":
               s = mt["DefaultValue"]
            if s == "":
               s = default
            assert(s)
            if s[0] == "(":
               s = s[1:-1] 
               t = map(typ, s.split(","))
               assert(len(t) == length)
               return tuple(t)
            else:
               t = map(typ, s.split(","))
               assert(len(t) == length)
               return tuple(t)
        else:
            assert(isinstance(s, tuple))
            return s

    #print value
    #assert(isinstance(value, str) or (isinstance(value, unicode)))

    if mt.isMetaEnum():
      return str(value)
      
    if name == "String":
      return str(value)
    elif name == "Integer":
      return int(value)
    elif name == "Boolean":
      if isinstance(value, bool):
        return value
      if value == "1" or value == "True":
        return True
      else:
        return False
    elif name == "Float":
      return float(value)
    elif name == "Picture":
      return str(value)
    elif name == "Text":
      return str(value)
    elif name == "DateTime" or name == "TimeStamp":
      return getTuple(value, int, 6, "(2000, 1, 1, 0, 0, 0)")
    elif name == "Color":
      return getTuple(value, int, 4, "(0, 0, 0, 0)")
    elif name == "Font":
      return value
    elif name == "Blob":
      return value
    elif name == "FileString":
      return value
    elif name == "Directory":
      return value
    elif name == "Size":
      return getTuple(value, float, 2, "(0.0, 0.0)")
    elif name == "Point":
      return getTuple(value, float, 2, "(0.0, 0.0)")
    elif name == "Rect":
      return getTuple(value, float, 4, "(0.0, 0.0, 0.0, 0.0)")
    elif name == "Flags":
      return int(value)
    elif name == "Url":
      return value
    else:
      print name, value
      assert(0)

""" List of all possible types
String
Integer
Boolean
Float
Picture
Text
DateTime
TimeStamp
Color
Font
Blob
FileString
Directory
Size
Point
Rect
Flags
Url
"""

# Add here mapping between the MM types and all other kind of information
mappings = {}
